//
//  ClubIntegralStoreViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/11.
//

import UIKit

class ClubIntegralStoreViewController: ClubBaseViewController {

    private var topView = ClubIntegralStoreTopView()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private var emptyView = ClubEmptyView()
    
    private var allData = [ClubMallListModel]()
    private var sourceData = [ClubMallListModel]()
    private var pageIndex = 1
    private var type = 100
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
        
        view.subviews{
            topView
            collectionView
            emptyView
        }
        
        topView.leading(0).trailing(0).top(0).height(150)
        collectionView.leading(0).trailing(0).bottom(0).top(162)
        emptyView.leading(0).trailing(0).height(195.0.Scale)
        emptyView.Top == topView.Bottom + 200.0.Scale
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 12
        layout.minimumInteritemSpacing = 12
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: (.screenWidth - 36.0.Scale)/2, height: 240.0.Scale)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12.0.Scale, bottom: 0, right: 12.0.Scale)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(ClubMallGoodsCell.self, forCellWithReuseIdentifier: ClubMallGoodsCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        emptyView.isHidden = true
        emptyView.titleString = "暂无商品~"
        
        topView.backBlock = {
            self.navigationController?.popViewController(animated: true)
        }
        topView.infoBlock = {
            self.navigationController?.pushViewController(ClubExchangeDetailViewController(), animated: true)
        }
        topView.updateCateBlock = { type in
            
            if type == 100 {
                //全部
                self.sourceData = self.allData
            }else if type == 101 {
                //虚拟
                self.sourceData.removeAll()
                for md in self.allData {
                    if md.type == "0" {
                        self.sourceData.append(md)
                    }
                }
            }else if type == 102 {
                //实物
                self.sourceData.removeAll()
                for md in self.allData {
                    if md.type == "1" {
                        self.sourceData.append(md)
                    }
                }
            }
            
            self.collectionView.reloadData()
        }
        
        collectionView.refresh_loadMore {
            self.loadMoreData()
        }.refresh_pull {
            self.loadNewData()
        }
    }
    
    @objc private func loadNewData(){
        pageIndex = 1
        loadData()
    }
    
    @objc private func loadMoreData(){
        pageIndex += 1
        Task {
            Task {
                let res = await network.request(.mallList(page: pageIndex),type: ClubMallListModel.self)
                collectionView.refresh_end()
                if res.isSuccess, let models = res.models {
                    for md in models {
                        if type == 100 {
                            //全部
                            self.sourceData.append(md)
                        }else if type == 101 {
                            //虚拟
                            if md.type == "0" {
                                self.sourceData.append(md)
                            }
                        }else if type == 102 {
                            //实物
                            if md.type == "1" {
                                self.sourceData.append(md)
                            }
                        }
                    }
                    self.allData.append(contentsOf: models)
                    self.collectionView.reloadData()
                }
            }
        }
    }
    
    private func loadData(){
        
        Task {
            let res = await network.request(.mallList(page: pageIndex),type: ClubMallListModel.self)
            collectionView.refresh_end()
            if res.isSuccess, let models = res.models {
                for md in models {
                    if type == 100 {
                        //全部
                        self.sourceData.append(md)
                    }else if type == 101 {
                        //虚拟
                        if md.type == "0" {
                            self.sourceData.append(md)
                        }
                    }else if type == 102 {
                        //实物
                        if md.type == "1" {
                            self.sourceData.append(md)
                        }
                    }
                }
                self.allData = models
                self.collectionView.reloadData()
            }
        }
    }

}

extension ClubIntegralStoreViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        emptyView.isHidden = !(sourceData.count == 0)
        return sourceData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ClubMallGoodsCell.description(), for: indexPath) as? ClubMallGoodsCell else { return UICollectionViewCell() }
        cell.model = sourceData[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let md = sourceData[indexPath.row]
        let detailVC = ClubIntegralDetailViewController()
        detailVC.goodsId = md.goods_id
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
}
